package com.jt.assistant.ui

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.ToastUtils
import com.jt.assistant.R
import com.jt.assistant.base.GlobalConfig.SCAN_TYPE_ONESHOT
import com.jt.assistant.bean.Param
import com.jt.assistant.viewmodel.HomeViewModel
import com.yzp.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_entrust_book.*

/**
 * 委托书查看
 */
class EntrustBookActivity : BaseActivity<HomeViewModel>() {

    override fun onDestroy() {
        set_entrust.unregisterEventBus()
        super.onDestroy()
    }

    override fun layoutId(): Int = R.layout.activity_entrust_book

    override fun initView(savedInstanceState: Bundle?) {
        set_entrust.initCamera(this, SCAN_TYPE_ONESHOT)
    }

    override fun initData() {

    }

    @SuppressLint("SetTextI18n")
    override fun initListener() {
        set_entrust.setOnScanListener {
            if (it.isEmpty()) {
                ToastUtils.showShort("请输入箱码")
                set_entrust.clearData()
                return@setOnScanListener
            }
            val params = Param::class.java.newInstance()
            params.cartonCode = it
            viewModel.getEntrustBookByCartonCode(params)
        }

        viewModel.mGetEntrustBookByCartonCode.observe(this, {
            if (it.isSuccess()) {
                tv_info.text = "转运单号: " + it.data.transNo + "\n" +
                        "委托书号: " + it.data.entrustBookNo + "\n" +
                        "准运批次号: " + it.data.transBatches + "\n" +
                        "业务单号: " + it.data.jdOrderNo + "\n" +
                        "转运单箱数: " + it.data.num + "\n" +
                        "转运体积: " + it.data.volume + "\n" +
                        "转运单重量: " + it.data.weight + "\n" +
                        "委托书提货时间: " + it.data.entrustDeliverDate + "\n" +
                        "起点: " + it.data.start + "\n" +
                        "终点: " + it.data.end + "\n"
                tv_info.visibility = View.VISIBLE
            } else {
                speakString("箱码错误")
                tv_info.visibility = View.GONE
                clearData()
            }
        })
    }


    private fun clearData() {
        tv_info.text = ""
        set_entrust.clearData()
    }
}